package com.travelranking.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.travelranking.entity.CityEntity;
import com.travelranking.entity.CountryEntity;
import com.travelranking.entity.TravelEntity;


public class RankingDAO {
	
	@PersistenceContext
	private EntityManager entityManager;

	
	public void saveCountry(CountryEntity c){
		try{
			System.out.println("persistindo country: "+c.toString());
			entityManager.persist(c);
			System.out.println("persistencia sem excessoes");
		}catch (Exception e){
			System.out.println("falha: "+e.getMessage());
			throw new RuntimeException("Exception persistindo country.",e);
		}
	}
	@SuppressWarnings("unchecked")
	public List<CountryEntity> listCountry(){
		List<CountryEntity> result = null;
		try{
			Query nq = entityManager.createNamedQuery("listAllCountry");
			result = nq.getResultList();
		}catch (Exception e){
			System.out.println("falha: "+e.getMessage());
			throw new RuntimeException("Exception listando CountryEntity.",e);
		}
		return result;
	}
	
	
	public void saveTravel(TravelEntity t){
		try{
			System.out.println("persistindo travel: "+t.toString());
			entityManager.persist(t);
			System.out.println("persistencia sem excessoes");
		}catch (Exception e){
			System.out.println("falha: "+e.getMessage());
			throw new RuntimeException("Exception persistindo travel.",e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<TravelEntity> listTravel(){
		List<TravelEntity> result = null;
		try{
			Query nq = entityManager.createNamedQuery("getAllTravels");
			result = nq.getResultList();
		}catch (Exception e){
			System.out.println("falha: "+e.getMessage());
			throw new RuntimeException("Exception listando travel.",e);
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<CityEntity> listCity(){
		List<CityEntity> result = null;
		try{
			Query nq = entityManager.createNamedQuery("getAllCity");
			result = nq.getResultList();
			
		}catch (Exception e){
			System.out.println("falha: "+e.getMessage());
			throw new RuntimeException("Exception listando City.",e);
		}
		
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<CityEntity> listCityByCountry(CountryEntity country){
		List<CityEntity> result = null;
		CountryEntity liveEntity = null;
		
		try{
			liveEntity = entityManager.find(CountryEntity.class, country.getId());
			Query nq = entityManager.createNamedQuery("getCityByCountry");
			nq.setParameter("country", liveEntity);
			
			result = nq.getResultList();
			
		}catch (Exception e){
			System.out.println("falha: "+e.getMessage());
			throw new RuntimeException("Exception listCityByCountry().",e);
		}
		
		return result;
	}
	
	public void saveCity(CityEntity entity){
		try {
			entityManager.persist(entity);
			System.out.println("persistencia [City] sem excessoes");
			
		} catch (Exception e){
			throw new RuntimeException ("Falha ao persistir CityEntity.",e);
		}
	}
	
	
	/*
	 * Metodos de acesso do EntityManager
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
}
